package com.huawei123.hasee.cniao5store.mvp.interactor;

import com.huawei123.hasee.cniao5store.api.AppDetailApi;
import com.huawei123.hasee.cniao5store.api.IGetDataDelegate;
import com.huawei123.hasee.cniao5store.base.BaseActivity;
import com.huawei123.hasee.cniao5store.bean.AppDetailBean;
import com.huawei123.hasee.cniao5store.utils.JsonParseUtils;
import com.zhxu.library.http.HttpManager;
import com.zhxu.library.listener.HttpOnNextListener;

import javax.inject.Inject;

/**
 * 项目名称：Cniao5Store
 * 类描述：应用详情页面交互器
 * 创建人：luohao
 * 创建时间：2018/2/8 14:46
 * 修改人：luohao
 * 修改时间：2018/2/8 14:46
 * 修改备注：
 */

public class AppDetailInteractor {
    private IGetDataDelegate<AppDetailBean> mDelegate;

    @Inject
    public AppDetailInteractor() {


    }

    public void loadAppDetailData(BaseActivity activity, String packageName, IGetDataDelegate<AppDetailBean> delegate) {
        this.mDelegate = delegate;
        AppDetailApi appDetailApi = new AppDetailApi(listener, activity, packageName);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(appDetailApi);//执行网络请求
    }

    HttpOnNextListener listener = new HttpOnNextListener<AppDetailBean>() {

        @Override
        public void onNext(AppDetailBean appDetailBean) {
            mDelegate.getDataSuccess(appDetailBean);
        }

        @Override
        public void onCacheNext(String string) {
            super.onCacheNext(string);
            AppDetailBean appDetailBean = JsonParseUtils.parseAppDetailBean(string);
            mDelegate.getDataSuccess(appDetailBean);
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            mDelegate.getDataError(e.getMessage());
        }
    };

}
